Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Answers for OCJP / SCJP Online Test - Set 2

←Go Back to Online Practice Test

Ques.

What is the output for the below code?

class A extends Thread {
    int count = 0;
    public void run() {
        System.out.println("run");
        synchronized (this) {
            for(int i =0; i < 50 ; i++) {
                count = count + i;
            }
            notify();
        }
    }
}

 

public class Test {
    public static void main(String argv[]) {
        A a = new A();
        a.start();
        synchronized (a) {
            System.out.println("waiting");
            try {
                a.wait();
            } catch(InterruptedException e) {
            }
            System.out.println(a.count);
        }

    }
}


Option 1.

waiting run 1225

Option 2.

waiting run 0

Option 3.

waiting run and count can be anything

Option 4.

Compilation fails


Ans. Option 1

Please provide the reason below. The best provided reason will be displayed with your name(if you are logged-in) on website.
Enter Reason
 
Is it helpful? Yes No
©2024 WithoutBook